#include "ChatServer.h"


bool isuser(string s)
{
for(auto c:passwd)
if(c.first==s)
return 1;
return 0;
}
bool ispasswd(string name,string newpasswd)
{
if(passwd[name]==newpasswd)
return 1;
else return 0;
}
void adduser(string name,string newpasswd)
{
passwd[name]=newpasswd;
}
string DealLogin(int connfd)
{
User tmp;
string sbuff;
string name;
while(1)
{
sbuff=dealdata(connfd);
if(sbuff=="login")
{
write(connfd,sbuff.c_str(),sizeof(sbuff.c_str()));
sbuff=dealdata(connfd);
name=sbuff;
if(isuser(sbuff))
sbuff="ok";
else sbuff="error";
write(connfd,sbuff.c_str(),sizeof(sbuff.c_str())); 
if(sbuff=="error") continue;
sbuff=dealdata(connfd);
if(ispasswd(name,sbuff))
sbuff="ok";
else sbuff="error";
write(connfd,sbuff.c_str(),sizeof(sbuff.c_str())); 
if(sbuff=="ok") break;
}
else if(sbuff=="signup")
{
write(connfd,sbuff.c_str(),sizeof(sbuff.c_str()));
//cout<<"get user name"<<endl;

sbuff=dealdata(connfd);
name=sbuff;
sbuff="ok";
write(connfd,sbuff.c_str(),sizeof(sbuff.c_str())); 
//cout<<"get passwd"<<endl;
sbuff=dealdata(connfd);
//cout<<222<<endl;
adduser(name,sbuff);
sbuff="ok";
write(connfd,sbuff.c_str(),sizeof(sbuff.c_str())); 
cout<<name<<passwd[name]<<endl;
alluser[name]=tmp;
}
else {cout<<"client abnormal termination"<<endl;break;}
}
return name;
}

